﻿using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using com.fukefu.tools;

namespace com.fukefu.vcode
{

    public class VCODE
    {
        [DllImport("sdo.dll")]
        public static extern int LoadLibFromFile(string LibFilePath, string nSecret);
        [DllImport("sdo.dll")]
        public static extern bool GetCodeFromBuffer(int LibFileIndex, byte[] FileBuffer, int ImgBufLen, StringBuilder Code);
       
        int TypeIndex = 1;
        public VCODE(Site site)
        {
            if (site != null && File.Exists("字库\\" + site.Domain + ".lib"))
            {
                TypeIndex = LoadLibFromFile("字库\\" + site.Domain + ".lib", "123");
            }
        }
        public string Get(Image image, ImageFormat f)
        {
            return Get(MyFunction.ImageToBytes(image, f));
        }
        public string Get(Stream s)
        {
            if (s == null)
            {
                return "";
            }
            return Get(MyFunction.StreamToBytes(s));
        }
        public string Get(byte[] buff)
        {
            StringBuilder Result = new StringBuilder('\0', 256);
            GetCodeFromBuffer(TypeIndex, buff, buff.Length, Result);
            return Result.ToString();
        }
    }
}
